{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Tuning sklearn esitmator\n",
    "\n",
    "Below code shows how you can use the BO library to tune sklearn estimator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Iter 0, best metric: 0.492082\n",
      "Iter 1, best metric: 0.5526\n",
      "Iter 2, best metric: 0.5526\n",
      "Iter 3, best metric: 0.5526\n",
      "Iter 4, best metric: 0.5526\n",
      "Iter 5, best metric: 0.715855\n",
      "Iter 6, best metric: 0.862657\n",
      "Iter 7, best metric: 0.862657\n",
      "Iter 8, best metric: 0.862657\n",
      "Iter 9, best metric: 0.862657\n",
      "Iter 10, best metric: 0.869785\n",
      "Iter 11, best metric: 0.882127\n",
      "Iter 12, best metric: 0.882127\n",
      "Iter 13, best metric: 0.882127\n",
      "Iter 14, best metric: 0.882127\n",
      "Iter 15, best metric: 0.882127\n"
     ]
    }
   ],
   "source": [
    "# Copyright (C) 2020. Huawei Technologies Co., Ltd. All rights reserved.\n",
    "\n",
    "# This program is free software; you can redistribute it and/or modify it under\n",
    "# the terms of the MIT license.\n",
    "\n",
    "# This program is distributed in the hope that it will be useful, but WITHOUT ANY\n",
    "# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A\n",
    "# PARTICULAR PURPOSE. See the MIT License for more details.\n",
    "\n",
    "from sklearn.datasets import load_boston\n",
    "from sklearn.ensemble import RandomForestRegressor\n",
    "from sklearn.metrics import r2_score, mean_squared_error\n",
    "from hebo.sklearn_tuner import sklearn_tuner\n",
    "\n",
    "space_cfg = [\n",
    "        {'name' : 'max_depth',        'type' : 'int', 'lb' : 1, 'ub' : 20},\n",
    "        {'name' : 'min_samples_leaf', 'type' : 'num', 'lb' : 1e-4, 'ub' : 0.5},\n",
    "        {'name' : 'max_features',     'type' : 'cat', 'categories' : ['auto', 'sqrt', 'log2']},\n",
    "        {'name' : 'bootstrap',        'type' : 'bool'},\n",
    "        {'name' : 'min_impurity_decrease', 'type' : 'pow', 'lb' : 1e-4, 'ub' : 1.0},\n",
    "        ]\n",
    "X, y   = load_boston(return_X_y = True)\n",
    "result = sklearn_tuner(RandomForestRegressor, space_cfg, X, y, metric = r2_score, max_iter = 16)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'max_depth': 15,\n",
       " 'min_samples_leaf': 0.00011814573477638075,\n",
       " 'max_features': 'log2',\n",
       " 'bootstrap': False,\n",
       " 'min_impurity_decrease': 0.00010743041070558209}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
